/**
 * Created by wangx08 on 2015-06-11.
 */

define(function (require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    var authorizeInfo = require('authorize');
    var userInfo = authorizeInfo.getUserInfo();
    require("permission");
    require('global');

    exports.redirectTo = function(proxyUrl){
        var url = proxyUrl + "&token="+util.getToken();
        $ifram = $("#proxy_iframe");
        function getIframeHeight() {
            return $(window).height() - $('body>.top').outerHeight() - $('body>.footer').outerHeight() - 55;
        }
        $(window).resize(function () {
            $ifram.height(getIframeHeight());
        });
        $ifram.attr('src', url)
            .height(getIframeHeight());

        $('#pop_dialog').click(function () {
            var dialogContent = '    <div class="dialog_box p15 relative" id="dialog_write" style="width: 610px;">' +
                '        <div class="text-center">' +
                '            <p class="close"><button i="close" style="position: relative;top: -40px;right: -15px;" class="ui-dialog-close" title="取消">×</button></p>' +
                '            <img src="/public/images/pic/tu1_06.png" class="postion img1">' +
                '            <img src="/public/images/pic/tu1_03.png" class="postion img2">' +
                '            <p class="f14 mt20" style="line-height: 35px;">小蜜蜂产品用得不爽？<br>' +
                '                有更多行销拓客需求？<br>' +
                '                现在你有机会召唤云客产品经理，<br>' +
                '                把他们叫到现场，倾听你的吐槽和需求，甚至驻点案场，深度了解剖析你们行销拓客的痛点。<br>' +
                '                这一切，<br>' +
                '                都为了给你一个用得爽、用得尖叫的产品！' +
                '            </p>' +
                '            <table width="300" style="margin: 15px auto;" class="f14">' +
                '                <tbody>' +
                '                <tr>' +
                '                    <td align="right" class="form-cell w88 pt16" valign="top">联系人：</td>' +
                '                    <td class="pt12"><input type="text" name="user_name" class="input-text w215">' +
                '                    </td>' +
                '                </tr>' +
                '                <tr>' +
                '                    <td align="right" class="form-cell w88 pt16" valign="top">手机号：</td>' +
                '                    <td class="pt12"><input type="text" name="mobile" class="input-text w215">' +
                '                    </td>' +
                '                </tr>' +
                '                <tr class="err" style="display: none;">' +
                '                    <td align="right" class="form-cell w88 pt16" valign="top"></td>' +
                '                    <td class="pt12" style="text-align: left;"><span style="color: red;" class="errmsg"></span>' +
                '                    </td>' +
                '                </tr>' +
                '                </tbody>' +
                '            </table>' +
                '        </div>' +
                '    </div>';
            var retDialog = '    <div class="dialog_box p15 relative" id="dialog_msg" style="width: 510px;">'+
                '        <div class="text-center">'+
                '            <p class="close"><button i="close"  style="position: relative;top: -20px;right: -15px;" class="ui-dialog-close" title="取消">×</button></p>'+
                '            <div class="clearfix" style="width: 510px;margin: 0 auto;">'+
                '                <div class="fl pl30">'+
                '                    <p class="f14" style="line-height: 35px;">召唤已发送，云客行销拓客产品经理稍后将与你联系<br>'+
                '                        感谢你的参与！</p>'+
                '                </div>'+
                '                <div class="fl">'+
                '                    <img src="/public/images/pic/contact_p.png" class="ml20">'+
                '                </div>'+
                '            </div>'+
                '        </div>'+
                '    </div>';
            util.showDialog({
                content: dialogContent,
                button: [
                    {
                        id: 'ok',
                        value: '马上召唤',
                        autofocus: true,
                        callback: function () {
                            var params = {},errmsg=$('span.errmsg');
                            params.user_name = $('#dialog_write input[name=user_name]').val();
                            params.mobile = $('#dialog_write input[name=mobile]').val();
                            params.org_code = userInfo.orgCode;
                            params.org_name = userInfo.orgName;
                            params.token = util.getToken();

                            if (!params.user_name) {
                                errmsg.html('请输入联系人');
                                $('tr.err').show();
                                return false
                            }
                            if (!params.mobile.match(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/)) {
                                errmsg.html('请输入正确的电话号码');
                                $('tr.err').show();
                                return false
                            }

                            $.ajax({
                                type: 'POST',
                                data:params,
                                url: '/api/index.php?r=usersurvey/user/submit-user-info&token='+util.getToken(),
                                success: function(res) {
                                    if (res.retCode == 0) {
                                        util.showDialog({
                                            content: retDialog,
                                            button: []
                                        });
                                    } else {
                                        util.alert(res.retMsg||res.errMsg||'提交失败！');
                                    }
                                },
                                error: function (res) {
                                    util.alert(res.retMsg||res.errMsg||'提交失败！');
                                }
                            });
                        }
                    }, {
                        id: 'cancel',
                        value: '稍后再说',
                        callback: function () {

                        }
                    }
                ]
            });
        })
    };
});